import math


#  用泰勒展开求sin(X)的值  x inputted is in angle
# define a function get factorial value

def factorial(n):
    if n == 1 or n == 0:
        return 1
    else:
        return factorial(n - 1) * n


def get_sin_x(x):
    x = float(x)
    counter = 1  # will be 1 2 3 4 5 6 ...
    sin_sum = 0  # store the sin value
    while abs(x ** (2 * counter - 1) / factorial(2 * counter - 1)) >= 10 ** -6:
        if counter % 2 != 0:
            sin_sum = sin_sum + x ** (2 * counter - 1) / factorial(2 * counter - 1)
        else:
            sin_sum = sin_sum - x ** (2 * counter - 1) / factorial(2 * counter - 1)
        counter += 1
    return sin_sum


if __name__ == '__main__':
    value = math.radians(float(input('Input the angle value: ')))  # get rad value
    print("My solving method: ", get_sin_x(value))

    # solving in book
    x1 = float(input("请输入角度"))
    x = math.radians(x1)
    n = 0
    s = a = x
    while abs(a) >= 1e-6:
        a *= -x * x / (x * n + 3) / (2 * n + 2)
        n += 1
        s += a

    print("x = {}, sin(x) = {}".format(x1, s))  # x = 32.0, sin(x) = 0.5300980879877981
    # the result is different from the book
